#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);    
    QTextEdit *te = findChild<QTextEdit*>("textEdit");
    if(te)
    {
        te->setText("123");
        te->installEventFilter(this);
    }
}

Widget::~Widget()
{
    delete ui;
}

void Widget::enterEvent(QEvent *event)
{
    qDebug() << "-- mouse enter";
}

void Widget::leaveEvent(QEvent *event)
{
    qDebug() << "-- mouse leave";
}

void Widget::wheelEvent(QWheelEvent *event)
{
//    qDebug() << "-- mouse wheel move";
//    qDebug() << event->angleDelta();
}

void Widget::closeEvent(QCloseEvent *event)
{
    qDebug() << "-- mouse close";
    int res = QMessageBox::warning(this, "Tips", "Do you want to close window?", QMessageBox::Yes | QMessageBox::No);
    switch (res)
    {
        case QMessageBox::Yes:
            event->accept();
            break;
        case QMessageBox::No:
            event->ignore();
            break;
    }
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() == QEvent::Wheel)
    {
        qDebug() << "mouse wheel...";
    }
    QKeyEvent *keyEvt = (QKeyEvent*) event;
    if(keyEvt->key() == Qt::Key_Control)
    {
        qDebug() << "Ctrl is Down...";
    }
    return true;
}
